<?php
namespace Admin\Controller;

use Think\Controller;

class EquipmentController extends Controller {

    /**
     * 显示所有设备
     */
    public function index($page = 0) {
        obtain();
        $Model = M('Equipment');
        $count = $Model->count();
        $pagenum = ($page < 2 || (($page - 1) * 10) > $count) ? 0 : ($page - 1) * 10;
        $college = $Model->limit($pagenum, 10)->order('userAccount asc')->select();
        $this->assign('zy', $college->name);
        $this->assign('list', $college);
        $page = getPageHtml($page, $count, __ROOT__ . "/Admin/Equipment/Index");
        $this->assign('page', $page);
        $this->display('index');
    }

    /*
     * 批量导入用户
     */

    public function add() {
        obtain();
        if (empty($_FILES)) {
            $this->display('add');
            exit;
        }
        ini_set('memory_limit', '1024M');
        $config = array(
            'exts' => array('xlsx', 'xls'),
            'maxSize' => 3145728000,
            'rootPath' => "./Data/",
            'savePath' => 'Uploads/',
            'subName' => array('date', 'Ymd'),
        );
        $upload = new \Think\Upload($config);
        if (!$info = $upload->upload()) {
            $this->error($upload->getError());
        }
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.IOFactory");
        $file_name = $upload->rootPath . $info['photo']['savepath'] . $info['photo']['savename'];
        $extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION)); //判断导入表格后缀格式
        if ($extension == 'xlsx') {
            $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
            $objPHPExcel = $objReader->load($file_name, $encode = 'utf-8');
        } else if ($extension == 'xls') {
            $objReader = \PHPExcel_IOFactory::createReader('Excel5');
            $objPHPExcel = $objReader->load($file_name, $encode = 'utf-8');
        }
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow(); //取得总行数
        $highestColumn = $sheet->getHighestColumn(); //取得总列数

        for ($i = 2; $i <= $highestRow; $i++) {
            //看这里看这里,前面小写的a是表中的字段名，后面的大写A是excel中位置
            if (empty((String) $objPHPExcel->getActiveSheet()->getCell("B" . $i)->getValue())) {
                continue;
            }
            $data[] = array(
                'name' => (String) $objPHPExcel->getActiveSheet()->getCell("A" . $i)->getValue(),
                'place' => (String) $objPHPExcel->getActiveSheet()->getCell("B" . $i)->getValue(),
                'userAccount' => (String) $objPHPExcel->getActiveSheet()->getCell("C" . $i)->getValue(),
            );
        }
        if (M('Equipment')->addAll($data)) {
            $this->success('导入成功!');
        } else {
            $this->error('失败');
        }
    }
/*
 * 添加设备
 */
    public function addone() {
        obtain();
        $data = M('Equipment');
        $data->create();
//        return var_dump($data);
        if ($data->add()) {
            $this->error('添加成功！');
        } else {
            $this->error('添加失败');
        }
    }
    /*
     * 添加设备时查询管理员
     */
    public function addoneView() {
        $result = M('user')->field('userAccount,name')->select();
        $this->assign('list', $result);
        $this->display("addone");
    }

    /**
     * 二维码
     * @param type $id
     */
    public function qrcode($id) {
        $qrcode = new \Tool\Qrcode();
        if ($qrcode->qrcode($id)) {
            $this->success('二维码生成成功');
        } else {
            $this->error('二维码生成失败');
        }
    }

    /**
     * 初始化密码
     * [teacher description]
     * @return [type] [description]
     */
    public function initialize($number) {
        obtain();
        $t['pwd'] = encrypt($number);
        M('user')->where('userAccount=\'%s\'', $number)->save($t);
        $this->success('重置成功！密码为：' . $number);
    }

    /**
     * 初始化管理员密码
     * [teacher description]
     * @return [type] [description]
     */
    public function admin() {
        obtain();
        empty($_POST) ? exit : 1;
        $tea = filter_input(INPUT_POST, 'tea');
        $val = filter_input(INPUT_POST, 'val');
        ($val != 0 && $val != 1) ? exit : 1;
        if (M('user')->where(array('userAccount' => $tea))->save(array('identity' => $val))) {
            $this->ajaxReturn(array('val' => 1));
        } else {
            $this->ajaxReturn(array('val' => 0));
        }
    }

    /**
     * 
     * @param type $number
     */
    public function deleteEquipment($number) {
        obtain();
        if (M('Equipment')->where('equ_id=%d', $number)->delete()) {
            $this->success('成功删除该设备！');
        } else {
            $this->error('未能删除，请检查原因！');
        }
    }
//锁定
    public function locking($lock, $id) {
        obtain();
        if ($lock == 0) {
            $data['locking'] = 1;
        } else {
            $data['locking'] = 0;
        }
        if (M('Equipment')->where(array('equ_id' => $id))->save($data)) {
            $this->success('操作成功');
        } else {
            $this->error('未修改');
        }
    }

    /**
     * 下载
     * @param type $url
     * @param type $name
     */
    public function dow($file, $name) {
        header("Content-type:text/html;charset=utf-8");
        $file_name = base64_decode($file);
        $file_dir = './';
        $name = $name . '.' . end(explode('.', $file_name));
        import('ORG.Net.Http');
        $http = new \Org\Net\Http();
        $http->download($file_dir . $file_name, $name);
    }
    public function alterView($id) {
        obtain();
        $Model = M('user');
        $count = $Model->count();
        $pagenum = ($page < 2 || (($page-1)*10)>$count)?0:($page-1)*10;
        $college = $Model->limit($pagenum,10)->order('userAccount asc')->select();
        $this->assign('zy', $name);
        $this->assign('list', $college);
        $page = getPageHtml($page,$count,__ROOT__."/Admin/Equipment/AlterView");
        $this->assign('page',$page);
        $this->assign('id',$id);
        $this->display('alter');
        
    }
    public function alter1($id,$user) {
        obtain();
        $data['equ_id'] = $id;
        $data['userAccount'] = $user;
        $map['equ_id'] = $id;
        if(M('equipment')->where($map)->save($data)){
            $this->success('成功修改',U('Admin/Equipment/index'));
        }else{
            $this->success('未修改',U('Admin/Equipment/index'));
        }
    }
}
